# -*- coding: utf-8 -*-
import re
import traceback
import xlwings as xw
import os

from Utils import XLWingsUtil, TimeUtil, DirUtil

# 日志文件转成性能统计报表
# 需要修改的参数
FILE_PATH = os.path.join(DirUtil.getDesktopDir(),"log_0308.log") # 文件默认保存在桌面,双引号内填写对应的文件名即可
##############
def numStr2numInt(numStr)->int:
    numInt = 0
    if re.match(r"[\d\.]+[万亿]?", numStr):
        if numStr.endswith('万'):
            numInt = int(float(numStr.replace('万', '')) * 10000)
        elif numStr.endswith('亿'):
            numInt = int(float(numStr.replace('亿', '')) * 100000000)
        else:
            numInt = int(numStr)
    return numInt
def tileData2Dict(filedArr:list,titleArr:list,data:list):
    dictt = {}
    for i in range(len(filedArr)):
        try:
            ind = titleArr.index(filedArr[i])
            dictt[filedArr[i]]=data[ind]
        except:
            dictt[filedArr[i]]=None
    return dictt
def log2Xlsx01(filePath:str):
    with open(filePath,"r",encoding='u8') as f:
        arr = f.readlines()
    exColNum = 8
    narr = [[] for i in range(8+exColNum)]
    for i in range(len(arr)):
        if arr[i].find("/6_")>=0:
            try:
                tDiff = int(arr[i].split(":")[1].split(" ")[0])
                ind = int(arr[i].split("/6_")[0].split("_")[-1])
                if ind==3:
                    narr[6+exColNum].append("'"+arr[i].split(" ")[-1].strip('\n'))
                    narr[7+exColNum].append(arr[i].split(" ")[-2])
            except:
                # print("tDiff",i,arr[i])
                # print("base10:",arr[i].split(":")[1].split(" ")[0])
                # print("ind", i, arr[i])
                # print("base10:", arr[i].split(":")[1].split(" ")[0])
                tDiff = int(arr[i].split(":")[2].split(" ")[0])
                ind = int(arr[i].split("/6_")[0].split(" ")[-1])
                if ind==3:
                    narr[6+exColNum].append("'"+arr[i].split(" ")[-1].strip('\n'))
                    narr[7+exColNum].append(arr[i].split(" ")[-2])
            if ind>6+exColNum:
                print(i,arr[i])
            narr[ind-1].append(tDiff)
    # for i in range(len(narr)):
    #     print(len(narr[i]))
    #     print(narr[i])
    dir, fileName = os.path.split(filePath)
    newFileName = fileName.split(".")[0]+"_"+TimeUtil.getFormatTimeNow("%H%M%S")+".xlsx"
    newFilePath = os.path.join(dir,newFileName)
    try:
        app = xw.App(visible=False, add_book=False)
        wb = app.books.open(r"../Raw/SettleStatistic.xlsx")
        XLWingsUtil.updateXLbyCellJI(wb,narr)
        XLWingsUtil.statisticsXL(wb)
        wb.save(newFilePath)
    except Exception as e:
        print("yzlog traceback.format_exc():", traceback.format_exc())
    finally:
        wb.close()
        app.quit()
if __name__ == '__main__':
    pass
    # print(['a','b','c'].index('d'))
    filePath = FILE_PATH
    log2Xlsx01(filePath)